package com.tueye.web.controller.blog;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.tueye.base.util.Page;
import com.tueye.entity.Pictures;
import com.tueye.entity.user.GoodFriend;
import com.tueye.entity.user.ParticularUserInfo;
import com.tueye.entity.user.User;
import com.tueye.service.userblog.IUserBlogService;
import com.tueye.utils.SystemContext;
import com.tueye.utils.TuEyeConstant;
import com.tueye.utils.UUIDGenerator;
import com.tueye.utils.servlet.CaptchaServiceSingleton;

/**
 * 用户blog管理
 * @author hanxiaowei
 *
 */
@SuppressWarnings("unchecked")
@org.springframework.stereotype.Controller
public class UserBlogController {
	private Log log = LogFactory.getLog(UserBlogController.class);
	
	@Autowired
	@Qualifier("userBlogService")
	IUserBlogService userBlogService;
	
	@RequestMapping("/userblog.do")
	public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ModelAndView mav = new ModelAndView("/pages/topics/userblog/blogindex");
		String userid = request.getParameter("userid");
		
		 User user = userBlogService.getUserById(userid);
		 if(user == null)
			 return new ModelAndView("/pages/topics/usermanager/userlogin");
		 ParticularUserInfo pui = null;
		 //add by yuyue for user's particular information
		 if(user !=null && user.getParticularuserinfo() != null )
		 {   if(user.getParticularuserinfo().size() != 0)
			  pui = user.getParticularuserinfo().get(0);
		     else{
		    	 pui = new ParticularUserInfo();
				 
				 pui.setSex(3); 
		     }
		 } 
		Page page = userBlogService.pagedQueryByHQL(userid, SystemContext.getOffset(), SystemContext.getPagesize(), null);
	    
		Map map = new HashMap();
		map = userBlogService.getFlowerNum(userid);
		List goodFriendList =  new ArrayList();
		goodFriendList = this.userBlogService.getGoodFriendToUserInfo(userid);
		PagerModel pm = new PagerModel(); 
		if(page != null){
		pm.setTotal(page.getTotalCount());
		pm.setDatas((List<Pictures>)page.getResult());
		request.setAttribute("pm", pm);
	   }
		request.setAttribute("flowersNum", map.get("flowersNum"));//鲜花个数
		request.setAttribute("caiNum", map.get("caiNum"));//有才个数
		request.setAttribute("paiNum", map.get("paiNum"));//拍的好个数
		request.setAttribute("kouNum", map.get("kouNum"));//流口水个数
		request.setAttribute("zanNum", map.get("zanNum"));//赞一个数
		request.setAttribute("gfuser", goodFriendList);
		 
		request.setAttribute("user", user);
		request.setAttribute("pui", pui);
		return mav;
	}
	//管理中心
	@RequestMapping("/userset.do")
	public ModelAndView userSet (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ModelAndView mav = new ModelAndView("/pages/topics/usermanager/userinfoset");
		
		return mav;
	}
	//转到加为好友
	@RequestMapping("/leapgoodFriend.do")
	public ModelAndView leapgoodFriend(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String bloguserid = request.getParameter("bloguserid");
		String blogusername = request.getParameter("blogusername");
		request.setAttribute("bloguserid", bloguserid);
		request.setAttribute("blogusername", blogusername);
		ModelAndView mav = new ModelAndView("/pages/topics/userblog/goodFriend");
		return mav;
	}
	 
	 
	@RequestMapping("/saveGoodFriend.do")
	public String saveGoodFriend(HttpServletRequest request, HttpServletResponse response){
		
		String validateCode = request.getParameter("validateCode");
		String content = request.getParameter("content");
		String bloguserid = request.getParameter("bloguserid");
		String blogusername = request.getParameter("blogusername");
		response.setContentType("html/txt");
		response.setCharacterEncoding("utf-8");
		User user = (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
		try{
			boolean flag = CaptchaServiceSingleton.getInstance().validateCaptchaResponse(validateCode, request.getSession());
			if(!flag){
				response.getWriter().write("0");
			}else{
				GoodFriend goodFriend = new GoodFriend();
				goodFriend.setId(new UUIDGenerator().getUUID());
				goodFriend.setSys_time(new Date());
				goodFriend.setFriend_id(bloguserid);
				goodFriend.setUser_id(user.getId());
				goodFriend.setContent(content);
				goodFriend.setAccept_req(2);
				this.userBlogService.saveGoodFriend(goodFriend);
				response.getWriter().write("1");
			}
			
		}catch (Exception e) {
			log.error(e.getMessage());
			
		}
		return null;
	}
	@RequestMapping("/isGoodFriend.do")
	public String isGoodFriend(HttpServletRequest request, HttpServletResponse response){
		String userid = request.getParameter("bloguserid");
		response.setContentType("html/txt");
		response.setCharacterEncoding("utf-8");
		try {
			String retuln = this.userBlogService.isGoodFriend(userid);
			if(retuln.equals("1")){
				//审核
				response.getWriter().write("2");
			}else if(retuln.equals("2")){
				//接受
				response.getWriter().write("1");
			}else if(retuln.equals("3")){
				//拒绝
				response.getWriter().write("3");
			}else{
				response.getWriter().write("0");
			}
			
		} catch (Exception e) {
			log.equals(e.getMessage());
		}
		return null;
	}
}
